package net.sxjr.protal.igcadre.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import net.sxjr.common.Response;
import net.sxjr.mbg.entity.IgCadreInspection;
import net.sxjr.mbg.entity.IgCadreInspectionDetail;
import net.sxjr.mbg.entity.IgCadrePromoteRecruit;
import net.sxjr.mbg.mapper.IgCadreInspectionDetailMapper;
import net.sxjr.mbg.mapper.IgCadreInspectionMapper;
import net.sxjr.mbg.mapper.IgCadrePromoteRecruitMapper;
import net.sxjr.protal.igcadre.params.IgcadreInspectionParams;
import net.sxjr.protal.igcadre.params.igCadrePromoteRecruitParms;
import net.sxjr.protal.mapconverter.Converter;
import net.sxjr.protal.publicservice.PublicService;
import net.sxjr.protal.user.dao.LoginUserInfo;
import net.sxjr.protal.user.dao.UserInfo;
import net.sxjr.protal.user.service.LoginService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author ：cuixc
 * @date ：Created in 2020/12/12 14:59
 * @description：
 * @modified By：
 */
@Service
public class CadrePromoteRecruitService {
    @Autowired
    private LoginService loginService;
    @Autowired
    private PublicService publicService;
    @Autowired
    private IgCadrePromoteRecruitMapper cadrePromoteRecruitMapper;
    public Response up(igCadrePromoteRecruitParms parms) {
        LoginUserInfo currentUser = loginService.getCurrentUser();
        UserInfo userinfo = loginService.getUserinfo(currentUser.getAccount());
        IgCadrePromoteRecruit recruit = Converter.INSTANCE.parms2dao(parms);
        if(StringUtils.isBlank(recruit.getCadrePromoteRecruitId())){
            //创建 干部提拔任用
            recruit.setCreateOp(userinfo.getUserId());
            recruit.setCreateDate(new Date());
            recruit.setDeptId(userinfo.getDeptId());
            recruit.insert();
        }else {

            recruit.updateById();

            publicService.setLinkConfirm(recruit,loginService.getCurrentUser());

        }
        publicService.writeSmsManage(publicService.getLinkCode(recruit),recruit.getSuperviseCode());
        return Response.success(Converter.INSTANCE.cadre2parms(recruit));
    }

    public Response list() {
        List<IgCadrePromoteRecruit> recruits = cadrePromoteRecruitMapper.selectList
                (new LambdaQueryWrapper<IgCadrePromoteRecruit>().ne(IgCadrePromoteRecruit::getDeleteFlag,1)
                        .or().isNull(IgCadrePromoteRecruit::getDeleteFlag)
                        .orderByDesc(IgCadrePromoteRecruit::getSuperviseCode));
        List<igCadrePromoteRecruitParms> parms = Converter.INSTANCE.cadre2parms(recruits);

        return Response.success(parms);
    }

    public Response cadreInspectionUp(IgcadreInspectionParams parms) {
        LoginUserInfo currentUser = loginService.getCurrentUser();
        UserInfo userinfo = loginService.getUserinfo(currentUser.getAccount());
        IgCadreInspection igCadreInspection = Converter.INSTANCE.cadreInspectionParams2Dao(parms);
        if(StringUtils.isBlank(igCadreInspection.getCadreInspectionId())){
            //创建 干部提拔任用
            igCadreInspection.setCreateOp(userinfo.getUserId());
            igCadreInspection.setCreateDate(new Date());
            igCadreInspection.setDeptId(userinfo.getDeptId());
            igCadreInspection.insert();
        }else {
            IgCadreInspectionDetail detail = new IgCadreInspectionDetail();
            detail.setCadreInspectionDate(parms.getCadreInspectionDate());
            detail.setIsCadreInspection(parms.getIsCadreInspection());
            detail.setCadreInspectionId(parms.getCadreInspectionId());
            if(StringUtils.isBlank(parms.getCadreInspectionDetailId())&&"10402001".equals(parms.getCurrentLink())){
                    detail.setCreateDate(new Date());
                    detail.setCreateOp(userinfo.getUserId());
                    detail.insert();
                    parms.setCadreInspectionDetailId(detail.getCadreInspectionDetailId());

            }else{
                detail.setCadreInspectionDetailId(parms.getCadreInspectionDetailId());
                detail.updateById();
            }

            igCadreInspection.updateById();
            publicService.setLinkConfirm(igCadreInspection,loginService.getCurrentUser());

        }
        publicService.writeSmsManage(publicService.getLinkCode(igCadreInspection),igCadreInspection.getSuperviseCode());
        return Response.success(Converter.INSTANCE.cadreInspectionDao2Params(igCadreInspection));
    }

    @Autowired
    private IgCadreInspectionMapper cadreInspectionMapper;
    @Autowired
    private IgCadreInspectionDetailMapper cadreInspectionDetailMapper;
    public Response cadreInspectionList() {
        List<IgcadreInspectionParams> icps = new ArrayList<>();
        List<IgCadreInspection> cadreInspections = cadreInspectionMapper.selectList(
                new LambdaQueryWrapper<IgCadreInspection>().ne(IgCadreInspection::getDeleteFlag,1)
                        .or().isNull(IgCadreInspection::getDeleteFlag)
                        .orderByDesc(IgCadreInspection::getSuperviseCode));
        for (IgCadreInspection cadreInspection : cadreInspections) {
            IgcadreInspectionParams params = Converter.INSTANCE.cadreInspectionDao2Params(cadreInspection);
            IgCadreInspectionDetail detail = cadreInspectionDetailMapper.selectByIdLimit1(cadreInspection.getCadreInspectionId());
            if(detail != null){
                params.setCadreInspectionDate(detail.getCadreInspectionDate());
                params.setIsCadreInspection(detail.getIsCadreInspection());
                params.setCadreInspectionDetailId(detail.getCadreInspectionDetailId());
            }
            icps.add(params);
        }
        return Response.success(icps);
    }
}
